<template>
  <div class="detail">
    <el-backtop :bottom="60" :right="20" style="color: #ff5777"> </el-backtop>
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" />

    <el-carousel trigger="click" height="500px" class="carousel">
      <el-carousel-item v-for="item in topImages" :key="item">
        <img :src="item" alt="" />
      </el-carousel-item>
    </el-carousel>
    <detail-base-info :goods="goods" />
    <detail-shop-info :shop="shop" />
    <detail-goods-info :detailInfo="detailInfo" />
    <detail-params-info ref="params" :itemParams="itemParams" />
    <detail-comment-info ref="comment" :commentInfo="commentInfo" />
    <detail-recommends-goodlist
      ref="recommends"
      :goods="recommends"
      class="viewbottom"
    />
    <detail-bottom-bar @addCart="addToCart" />
    <toast :message="message" :show="show" />
  </div>
</template>

<script>
import { getDetail, Goods, Shop, getRecommends } from "network/detail";

import DetailNavBar from "./childComps/DetailNavBar";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetialGoodsInfo.vue";
import DetailParamsInfo from "./childComps/DetailParamsInfo.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailRecommendsGoodlist from "./childComps/DetailRecommendsGoodsList.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";

import Toast from "../../components/common/toast/Toast.vue";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamsInfo,
    DetailCommentInfo,
    DetailRecommendsGoodlist,
    DetailBottomBar,
    Toast,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      itemParams: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      message: "",
      show: false,
    };
  },
  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid;

    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      console.log(this.iid);
      const data = res.result;
      console.log(data);
      // 1.获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages;

      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      // 3.创建店铺信息的对象
      this.shop = new Shop(data.shopInfo);
      //4.获取商品详情展示图片
      this.detailInfo = data.detailInfo;

      //5.获取商品参数信息
      this.itemParams = data.itemParams;
      //6.获取商品的评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
      //所以的组件渲染完毕执行nexttick函数，
      this.$nextTick(() => {
        setTimeout(() => {
          this.themeTopYs = [];
          this.themeTopYs.push(0);
          this.themeTopYs.push(this.$refs.params.$el.offsetTop);
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
          this.themeTopYs.push(this.$refs.recommends.$el.offsetTop);
          this.themeTopYs.push(Number.MAX_VALUE);
          console.log(this.themeTopYs);
        }, 2000);
      });
    });
    //3.请求详情页里的推荐商品的数据
    getRecommends().then((res) => {
      this.recommends = res.data.list;
    });
  },
  updated() {},
  methods: {
    titleClick(index) {
      window.scrollTo(0, this.themeTopYs[index] - 44);
    },
    addToCart() {
      //1.获取购物车的要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      console.log(product);
      //2.把商品添加到购物车中
      // this.$store.commit("addCart", product)
      this.$store.dispatch("addCart", product).then((res) => {
        this.show = true;
        this.message = res;

        setTimeout(() => {
          this.show = false;
          this.message = "";
        }, 1500);
        console.log(res);
      });
    },
  },
};
</script>

<style scoped>
.detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
.detail-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background-color: #fff;
}
.carousel {
  padding-top: 40px;
}
.carousel img {
  width: 100%;
}
.viewbottom {
  padding-bottom: 56px;
}
</style>
